#include "google/protobuf/util/json_util.h"
#include "napi/native_api.h"
#include "svga.pb.h"
#include <hilog/log.h>
#include "chrono"
#include "createPixelMap.h"
#undef LOG_DOMAIN
#undef LOG_TAG
#define LOG_DOMAIN 0x3200
#define LOG_TAG "MY_TAG"
// 用于Base64编码
using com::opensource::svga::MovieEntity;

static napi_value Deserialize(napi_env env, napi_callback_info info) {
    auto start = std::chrono::high_resolution_clock::now();
    size_t requireArgc = 1;
    size_t argc = 1;
    napi_value args[1] = {nullptr};

    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    napi_value inputArray = args[0];

    // 获取ArrayBuffer类型
    napi_typedarray_type type;
    napi_value inArrayBuffer;
    size_t byteOffset;
    size_t length;
    napi_get_typedarray_info(env, inputArray, &type, &length, nullptr, &inArrayBuffer, &byteOffset);
    if (type != napi_uint8_array) {
        return nullptr;
    }

    // 获取ArrayBuffer信息
    void *data = nullptr;
    size_t byte_length;
    napi_get_arraybuffer_info(env, inArrayBuffer, &data, &byte_length);

    // 将arraybuffer反序列化为MovieEntity
    MovieEntity::env = env;
    MovieEntity movieEntity;
    if (movieEntity.ParseFromArray(data, byte_length)) {
        std::string jsonString;
        google::protobuf::util::MessageToJsonString(movieEntity, &jsonString);
        // 遍历images 
//         if (movieEntity.images_.size() > 0) {
//             napi_value images;
//             napi_create_object(env, &images);
//             for (auto it = movieEntity.images_.MapBegin(); it != myMap.end(); ++it) {
//                 napi_value nKey;
//                 napi_create_string_utf8(env, key.c_str(), key.size(), &nKey);
//             }
//         }
        napi_value response;
        napi_create_object(env, &response);
        napi_value str;
        napi_create_string_utf8(env, jsonString.c_str(), jsonString.size(), &str);
        napi_set_named_property(env, response, "str", str);
        napi_set_named_property(env, response, "images", movieEntity.napi_instance_images);
        OH_LOG_ERROR(LOG_APP, "解析时间222: %{public}s.", jsonString.c_str());

        // 反序列化成功
        // 记录结束时间
//     auto end = std::chrono::high_resolution_clock::now();
// //
// //     计算执行时间（以毫秒为单位）
//     auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
//     OH_LOG_ERROR(LOG_APP, "解析时间222: %{public}d.", duration);
//
//         return movieEntity.GetNodeJs();
        return response;
    }
    return nullptr;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
    napi_property_descriptor desc[] = {
        {"deserialize", nullptr, Deserialize, nullptr, nullptr, nullptr, napi_default, nullptr}};
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "svga",
    .nm_priv = ((void *)0),
    .reserved = {0},
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
